<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script type="text/javascript">
$(document).ready(function(){
	//일반 글쓰기 버튼클릭시	
	$("#writeBtn").on("click",function(){
		var category = $("#category").val();
		if(category==""){
			alert("게시판을 선택해주세요.");
			return;
		}
		
		var title = $("#title").val();
		if(title==""){
			alert("제목을 입력해주세요.");
			return;
		}
		
		var content = $("#content").val();
		if(content==""){
			alert("내용를 입력해주세요.");
			return;
		}		
		var flag=confirm("글작성을 완료 하시겠습니까?");		
		 if(flag){
			 var f = $("#write_form");
			 f.attr("action","${initParam.root }/board.do?mode=write&notice=nomal");
			 f.submit();
		 }		
	});	// writeBtn
	
	// 공지사항 등록시 추가
	$("#noticeBtn").on("click",function(){
		var category = $("#category").val();
		if(category==""){
			alert("게시판을 선택해주세요.");
			return;
		}	
		var title = $("#title").val();
		if(title==""){
			alert("제목을 입력해주세요.");
			return;
		}		
		var content = $("#content").val();
		if(content==""){
			alert("내용를 입력해주세요.");
			return;
		}		
		var flag=confirm("공지사항을 등록 하시겠습니까?");		
		 if(flag){
			 var f = $("#write_form");
			 f.attr("action","${initParam.root }/board.do?mode=write&notice=notice");
			 f.submit();
		 }
	}); // noticeBtn
});



function reviewUploadImg(fileObj)
{
 var filePath = fileObj.value;
 var fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
 var fileKind = fileName.split(".")[1];
 
 if($("#uploadFile")[0].files[0].size>3145728){
		alert("3MB 이하로 업로드해주세요!!");
		$("#main_uploadImg").val("");
		return;
	}
 if(fileKind != "jpg" && fileKind != "gif" && fileKind != "png")
 {
  alert("jpg, gif, png 확장자를 가진 이미지 파일만 올려주세요.");
  document.getElementById("image_file").value = "";
  document.getElementById("image_file").select();
  document.selection.clear();
 }else{
	
	 
	 var uploadData = $("#uploadFile").prop("files")[0];
	 var newData = new FormData();

	 $.each($('#uploadFile').prop("files"), function(i, file) {
	     newData.append('uploadFile', file);
	 });
	 newData.append("mode","thumbImg");
	 $.ajax({

	     type        : 'POST',
	     url     : '${initParam.root }/board.do?mode=thumbImg&thumDel='+$("#thumDel").val(),
	     data        : newData,
	     contentType: false,
	     cache: false,
	     processData: false,                        
	     success     : function(r) {
	    	 var i = "<img src='/img/thumb_bo/";
	    	 i+=r.img;
	    	 i+="'></img>";
	    	 i+="<input type='hidden' id='thumDel' value='"+r.img+"'>"; // 생성된 썸네일이미지를 지우기위해 필요
	         $("#previewImg").html(i);
	     },
	     error       : function(r) { alert('jQuery Error'); }
	 });
 }
}

</script>

<c:choose>
	<c:when test="${sessionScope.mvo.uId==null }">
	<script type="text/javascript">
			alert("로그인을 하셔야합니다!");
			location.href="${initParam.root }/index.jsp";
		</script>
	</c:when>
<c:otherwise>
<h3>글쓰기</h3>

  <form action="${initParam.root }/board.do" method="post" id="write_form" enctype="multipart/form-data">
   <table width="70%" align="center">
    <tr>
     <td width="10%">제목</td>
     
     <!-- 현석 공지 추가 시작-->
     <c:choose>
		<c:when test="${sessionScope.mvo.level=='99' }">
			<td width="10%">
				<select name="category" id="category">
					<option value="">-게시판선택-</option>
					<option value="tip">팁게시판</option>
					<option value="free">자유게시판</option>
					</select>
			</td>
		</c:when>
		
		<c:otherwise>
			<input type="hidden" name="category" id="category" value="${param.category }">
		</c:otherwise>
	</c:choose>
	<!-- 현석 공지 추가 끝-->	
     
     <td colspan="1">
     <input type="text" id="title" maxlength="200" size="50" name="title">
     </td>
    </tr>
    <tr>
    	<td colspan="4">
    	<input type="file" name="uploadFile" id="uploadFile" size="60"  onchange="reviewUploadImg(this);"/>
    	</td>
    </tr>
    <tr>
    	<td colspan="4">
    		<div id="previewImg"></div>
    	</td>	
    </tr>

    <tr>

     <td colspan="4">
     <textarea cols="80" rows="20" id="content" name="contents"></textarea>
     </td>
    </tr> 
    <tr>
     <td colspan="2" align="left" >
     	<input type="button" id="writeBtn" value="글쓰기">
     	  <!-- 공지 추가 -->
     	  <c:if test="${sessionScope.mvo.level=='99' }">   	
     	     	<input type="button" id="noticeBtn" value="공지사항등록">
     	  </c:if>
     	  <!-- 공지 추가 끝-->  
     </td>
  
    </tr>
   </table>
  </form>
  </c:otherwise>
</c:choose>
